package com.xlh.controller;

/**
 * @author cfz
 */

import com.xlh.enums.ExcelTemplateEnum;
import com.xlh.exception.common.GlobalException;
import com.xlh.service.ExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 工程认证 - 课程达成度
 * <p>
 * chufangzheng
 */
@Api(tags = "ExcelTemplateController", description = "excel 模板下载公共类")
@RestController
@RequestMapping("/excel/template")
public class ExcelTemplateController {

    @Autowired
    private ExcelService excelService;

    @ApiOperation("公共excel模板下载")
    @GetMapping("/teadown")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public void teacherToDownTemp(HttpServletRequest request, HttpServletResponse response, String tempCode){

        ExcelTemplateEnum template = ExcelTemplateEnum.getEnumByCode(tempCode);
        try {
            excelService.downloadExcelModel( response, request,  template.getName(), template.getFilePath());
        } catch (IOException e) {
            throw new GlobalException("找不到指定的excel文件");
        }
    }


}
